home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
render2pixmap
/
offscreen_render.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
6KB
|
225 lines
/*
* Copyright (c) 1993-94, Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the name of Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
* POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
/*
* 1993 Simon Hui -- Silicon Graphics Computer Systems
*
* Test drawing to pixmaps of various sizes.
*
* Modified to be part of an opengl render to file test case
* by Ann LaGrone -- Silicon Graphics Computer Systems, 1994.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <X11/keysym.h>
#include "util.h"
/*
* Size of pixmap to create,
*/
int size = 300; /* pixmap size, this example is square */
#define GAP 10 /* border */
long width, height;
XVisualInfo *vi;
GC xgc;
Display *dpy;
Window window;
Pixmap pix;
GLXPixmap glxpix;
GLXContext cx;
/* Scene drawing routine */
static void drawScene(void)
{
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
/*
* Draw the right triangle.
*/
glColor4f(0., 1., 0., 1.);
glBegin(GL_TRIANGLES);
glVertex3f( 0.9, -0.9, -30.0);
glVertex3f( 0.9, 0.9, -30.0);
glVertex3f(-0.9, 0.0, -30.0);
glEnd();
/*
* Draw the left triangle.
*/
glColor4f(0., 0. ,1., .2);
glBegin(GL_TRIANGLES);
glVertex3f(-0.9, -0.9, -40.0);
glVertex3f(-0.9, 0.9, -40.0);
glVertex3f( 0.9, 0.0, -25.0);
glEnd();
}
/*
Switch contexts to offscreen rendering and output the
image to a pixmap, then call writeimage to dump it to
a file in sgi image file format.
*/
static void tofile(void)
{
int i=0, x=GAP, y=GAP;
XImage *ximage;
XWindowAttributes win_info;
XClearWindow(dpy, window);
glXMakeCurrent(dpy, glxpix, cx);
glViewport(0, 0, size, size);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawScene();
glXWaitGL();
ximage = XGetImage(dpy, pix, 0, 0, size, size, 0xffffff,
ZPixmap);
/* Set up mask fields for writeimage program */
if(!XGetWindowAttributes(dpy, window, &win_info)) {
printf("Can't get target window attributes.");
exit(1);
}
ximage->red_mask = win_info.visual->red_mask;
ximage->green_mask = win_info.visual->green_mask;
ximage->blue_mask = win_info.visual->blue_mask;
/* write out image in SGI image file format */
writeimage(ximage, "offscreen.rgb");
/* Copy the pixmap image to the screen */
XCopyArea(dpy, pix, window, xgc, 0, 0,
size, size, x, y);
glFlush();
XFlush(dpy);
}
/* Standard redraw routine to the screen's drawable */
static void redraw(void)
{
int i=0, x=GAP, y=GAP;
XImage *ximage;
XClearWindow(dpy, window);
glXMakeCurrent(dpy, window, cx);
glViewport(0, 0, size, size);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawScene();
glFlush();
XFlush(dpy);
}
static int attributes[] = {
GLX_RGBA,
GLX_RED_SIZE, 1,
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
GLX_DEPTH_SIZE, 1,
None,
};
/* input events -
esc quit/exit
left arrow redraw
right arrow off screen render to file
*/
void keypress( KeySym ks, Bool *exit, Bool *redraw, Bool *tofile )
{
switch (ks) {
case XK_Left:
*redraw = True;
break;
case XK_Right:
*tofile = True;
break;
case XK_Escape:
*exit = True;
break;
}
}
int main(int argc, char **argv)
{
XEvent event;
GLboolean needDisplay;
int i, total, maxwidth;
/*
* Make the window big enough to hold all the pixmaps.
*/
total = GAP;
maxwidth = 0;
total += size + GAP;
if (size > maxwidth) {
maxwidth = size;
}
width = total;
height = maxwidth + 2*GAP;
window = utilCreateWindow( width, height, 50, 50, None, attributes, "Gray",
argc, argv, NULL, &dpy, &vi, NULL, &xgc);
cx = glXCreateContext(dpy, vi, 0, False);
if (!glXMakeCurrent(dpy, window, cx)) {
fprintf(stderr, "Can't make window current to context\n");
return -1;
}
pix = XCreatePixmap(dpy, window, size, size, vi->depth);
glxpix = glXCreateGLXPixmap(dpy, vi, pix);
redraw();
utilEventLoop(dpy, NULL, keypress, redraw, tofile);
glXMakeCurrent(dpy, None, NULL);
glXDestroyContext(dpy, cx);
XCloseDisplay(dpy);
}